﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using 纺织贸易管理系统;

namespace Update
{
  public  class VersionHelper
    {
        public static Version GetCurrentVersion()
        {
            var infos = Connect.GetSugar().Queryable<info>().ToList ();
            if (infos.Count > 0)
            {
                var info = infos[0];
                if (info == null)
                {
                    info = new info() { Version = "1.0.2.12" };

                }
                info.Version = string.IsNullOrWhiteSpace(info.Version) ? "1.0.2.12" : info.Version;
                return Version.Parse(info.Version);
            }
            else
            {
                return Version.Parse("1.0.2.12");
            }
        }
        /// <summary>
        /// 更新版本号
        /// </summary>
        /// <param name="newversion">新的版本号</param>
        public static void  IncreaseVersion()
        {
            string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            XDocument document = XDocument.Load(currentDirectory + "\\Version.xml");
            XElement root = document.Root;
            XElement updater = root.Element("Updater");
            XElement ver = updater.Element("MainVersion");
            Version localVer = Version.Parse(ver.Value);
            var newinfo= Connect.dbcontext.Queryable<info>().First();
            newinfo.Version = localVer .ToString();
            Connect.dbcontext.Updateable(newinfo).ExecuteCommand();           
            Console.WriteLine ("数据库版本更新到"+ localVer.ToString());
        }
    }
}
